#include "mycanvas.h"

MyCanvas::MyCanvas(Scene *_scene) {
    this->scene = _scene;
    this->ctrl = false;
    this->pointSize = 1;
}

void MyCanvas::onPressCursor(Cursor &cursor){
    if (cursor.getType() == FINGER){
        traces[cursor.getId()] = Trace(cursor.getId());
        
        Viewport &view = this->scene->getViewport();
        Window &win = this->scene->getWindow();
        Point scenePoint( win.left + (cursor.getX() * (win.right - win.left) 
                                                        / float(view.width) ),
                          win.top+( cursor.getY() * (win.bottom - win.top) 
                                                        / float(view.height) ) );

        cursor.setScenePoint(scenePoint);
        cursor.setPressure(this->pointSize);
        traces[cursor.getId()].addCursor(cursor);    
    } 
}


void MyCanvas::onMoveCursor(Cursor &cursor){
    if (cursor.getType() == FINGER){
        Viewport &view = this->scene->getViewport();
        Window &win = this->scene->getWindow();
        Point scenePoint( win.left + (cursor.getX() * (win.right - win.left) 
                                                        / float(view.width) ),
                          win.top+( cursor.getY() * (win.bottom - win.top) 
                                                        / float(view.height) ) );

        cursor.setScenePoint(scenePoint);
        cursor.setPressure(this->pointSize);
        traces[cursor.getId()].addCursor(cursor);   
    } 
    else if (cursor.getType() == FIDUCIAL){
        this->pointSize = int(10 * cursor.getAngle() / 6.28);
    }    
}


void MyCanvas::onPressKey(int key){
    if (key == KEY_CTRL) this->ctrl = true;
    if (key == KEY_S and this->ctrl) saveImage();
    if (key == KEY_N and this->ctrl) traces.clear();
}


void MyCanvas::onReleaseKey(int key){
    if (key == KEY_CTRL) this->ctrl = false;
}


void MyCanvas::paint(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1.0f, 0.0f, 0.0f);

    for (map<int,Trace>::iterator it = traces.begin(); it != traces.end(); it++){
        vector<Cursor> &cs = it->second.getCursors();
            for (unsigned int i=0; i < cs.size(); i++){
                glPointSize(int (cs[i].getPressure()));
                glBegin(GL_POINTS);
                glVertex2f( cs[i].getScenePoint().getX(), cs[i].getScenePoint().getY() );
                glEnd();
            }
    }
    glFlush();
}


void MyCanvas::initialize(){
    glClearColor(1.0, 1.0, 1.0, 1.0);
}

void MyCanvas::resizeCanvas(int _width, int _height){
    Viewport &viewport = this->scene->getViewport();
    Window &window = this->scene->getWindow();

    if (_width  == 0) _width  = 1;
    if (_height == 0) _height = 1;
    viewport.width = _width;
    viewport.height = _height;
    
    glViewport(viewport.left, viewport.top, viewport.width, viewport.height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    glOrtho(window.left, window.right, window.bottom, window.top, -10.0, 0.0);
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
